<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>产品</el-breadcrumb-item>
        <el-breadcrumb-item>DAU阅读情况</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
        <!-- <template slot="reading_rate" slot-scope="scope">
          {{(scope.row.total_user_num/scope.row.total_dau_num).toFixed(2)}}
      </template> -->
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { handleExcelCat } from "@/api/exportCommon";
import { productData as baseURL ,selectVersion} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        type: 0,
      },
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        border: true,
        menu: false,
        column: [
          {
            label: "日期",
            prop: "statistics_date_search",
            type: "daterange",
            search: true,
            hide:true
          },
          {
            label: "日期",
            prop: "create_time",
            type: "date",
            format:"yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: false,
          },
          {
            label: "客户端",
            prop: "source",
            type: "select",
            search: true,
            dicData: [
               {
                label: "安卓",
                value: 0,
              },
             {
                label: "IOS",
                value: 1,
              },
            ],
          },
          {
            label: "版本号",
            prop: "version",
            type: "input",
            search: true,
          },
          {
            label: "累计用户数",
            prop: "total_user_count",
            type: "input",
          },
          {
            label: "DAU总数",
            prop: "dau_count",
            type: "input",
          },
          {
            label: "阅读用户数",
            prop: "user_count",
            type: "input",
          },
          {
            label: "阅读率",
            prop: "read_rate",
            type: "number",
          },
          {
            label: "阅读图书次数",
            prop: "book_count",
            type: "input",
          },
          {
            label: "阅读章节数",
            prop: "chapter_count",
            type: "input",
          },
          {
            label: "总阅读时长(分钟)",
            prop: "read_time",
            type: "input",
          },
          {
            label: "人均阅读章节数",
            prop: "average_read_chapter_count",
            type: "input",
          },
          {
            label: "人均阅读时长(分钟)",
            prop: "average_read_time",
            type: "input",
          },
           {
            label: "阅读留存率",
            prop: "read_retention_rate",
            type: "input",
          },
           {
            label: "启动次数",
            prop: "start_count",
            type: "input",
          },
           {
            label: "启动留存",
            prop: "start_retention",
            type: "input",
          },
        ],
      },
    };
  },
  methods: {
    //数据导出
    async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = { ...this.$refs.crud.searchForm };
      params.route = "数据/产品/DAU阅读概况";

      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000; 

      if(params.statistics_date_search.length>0){
        export_condition += " &时间区间："+params.statistics_date_search[0].toLocaleDateString()+"~"+params.statistics_date_search[1].toLocaleDateString()
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }
      if(params.source){
        export_condition += " &客户端："+this.option.column[2].dicData.filter(source => source.value == params.source)[0].label;
      }
      if(params.book_id){
        export_condition += " &版本号："+params.version;
      }

      params.export_condition = export_condition == "" ? "所有" : export_condition;

     //调用导出接口
      let response = await common(
        baseURL.exportDauReadStatistics,
        { ...params, ...this.default },
        null,
        "POST"
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getReadActionChannelListByType,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if(params.statistics_date_search){
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }

      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>